$(function () {
    init_setlabel();
})
/**初始化标签**/
function init_setlabel() {
    /**初始化标签得内容**/
    $.post(ctx+"saleConfig/selectSaletag",{},function (r) {
        if (r.code == 0){
            var data = r.msg;
            $.each(data,function (index,val) {
                var pid = val.parent.tagUuid;
                var div =  $(' <div class="text-left border-bottom-1" pid="'+pid+'" typeid="1"></div>');
                var fistdiv  ='<div class="border-bottom-1 p-t-3"><div class="checkbox m-0 "><span class="m-r-15 m-l-5">'+val.parent.tagGroupName+'</span>' +
                    '<select class="width-200 form-control  inline-block m-b-3" onchange="selectLabelType(this)" ><option value="1">保持本组原有得标签</option>' +
                    '<option value="2">不添加本组标签,删除原有标签</option><option value="3">使用新的标签替换本组标签</option></select>' +
                    '<label><input type="hidden" value='+val.parent.tagSelect+' /></label></div></div>';
                div.append(fistdiv);
                var cc = $("<div class='m-t-5 p-3 text-left'><input type='hidden' value="+val.parent.tagSelect+" /></div>");
                $.each(val.children,function (i,v) {
                    cc.append('<button type="button" disabled onclick="selectTagLabel(this)" data-id="'+v.tagUuids+'" ' +
                        'class="btn btn-xs btn-link width-100 m-l-5 p-2 m-b-5 f-s-12 border-solid-g">'+v.tagName+'</button>');
                });
                div.append(cc);
                $("#selectTagSetLabel").append(div);
            });
        }else{
            $JuXue.n_danger(r.msg);
        }
    });
    /**初始化选择得人**/
    var ids = $("#myEmpTable").bootstrapTable("getSelections");
    if (!ids.length) {$JuXue.n_warning("请勾选需要设置的客户信息！");return;}
    for (var i = 0; i < ids.length; i++) {
        var type_arr = ids[i].types;; //uuid
        var uuid = ids[i].uuid;//类型
        var  name = ids[i].nickname;
        var  idsAll = ids[i].id;
        var li = ' <li types="'+type_arr+'" uuid="'+uuid+'" idsAll="'+idsAll+'">\n' +
        ' <div class="todolist-input"><a href="#" onclick="$(this).parent().parent().remove();">' +
         '<i class="fa fa-bitbucket-square"></i></a></div><div class="todolist-title">'+name+'</div></li>';
        $("#todolist").append(li);
    }
}

/**切换选择类型**/
function selectLabelType(btn) {
    var option = $(btn).val();
    if (option == "1" ||option =="2"){
        var allbutton = $(btn).parent().parent().next();
        allbutton.find("button").each(function () {
           $(this).attr("disabled","disabled");
           $(this).removeClass("btn-primary");
        });
    }else {
        var allbutton = $(btn).parent().parent().next();
        allbutton.find("button").each(function () {
            $(this).removeAttrs("disabled");
        });
    }
    $(btn).parent().parent().parent().attr("typeid",option);
}

/**选择分组**/
function selectTagLabel(btn) {
    var $parentcheck  = $(btn).parent();
    var isMultiInput = $parentcheck.children("input:first-child").val();
    if (isMultiInput == "1"){
        //多选
        var ishave = $(btn).hasClass("btn-primary");
        if (ishave) {
            $(btn).removeClass("btn-primary");
        }else{
            $(btn).addClass("btn-primary");
        }
    }else{
        //单选
        var ishave = $(btn).hasClass("btn-primary");
        if (ishave) {
            $parentcheck.find("button").each(function () {
                $(this).removeClass("btn-primary");
            });
        }else{
            $parentcheck.find("button").each(function () {
                $(this).removeClass("btn-primary");
            });
            $(btn).addClass("btn-primary");
        }
    }
}
/**整体追加标签保存**/
function saveLabelTag() {
    var div = $("#selectTagSetLabel").children("div");
    var selectids = new Array();

    $.each(div,function (index,val) {
        var btn = $(this).find("button");
        $.each(btn,function () {
            var isSelect = $(this).hasClass("btn-primary");
            if (isSelect){
                var selectid = $(this).attr("data-id");
                selectids.push(selectid);
            }
        });
    });
    /**选择类型**/
    var selectTypepid = new Array();
    /**typeid**/
    var selectTypeid = new Array();
    var labeldiv = $("#selectTagSetLabel").children("div");

    $.each(labeldiv,function () {
        var typeids= $(this).attr("typeid");
        selectTypeid.push(typeids);
        var pid= $(this).attr("pid");
        selectTypepid.push(pid);
    })


    var length = selectids.length;
    if(length > 10){$JuXue.n_danger("标签选择不能大于10个");return ;}
    //查询当前所有选择得客户
    var types = new Array();
    var uuids = new Array();
    var idsAlls = new Array();
    $("#todolist").find("li").each(function () {
        var type = $(this).attr("types"); //类型
        types.push(type);
        var uuid = $(this).attr("uuid") ;//uuid
        uuids.push(uuid);
        var idsAll  =$(this).attr("idsAll");// id
        idsAlls.push(idsAll);
    });
    if (types.length <= 0) {$JuXue.n_danger("获取数据失败");return}

    var uuidss = uuids.join(",");
    var typess = types.join(",");
    var idsAllss = idsAlls.join(",");
    var label = selectids.join(",");

    /**类型**/
    var pidstr = selectTypepid.join(",");
    var idstr = selectTypeid.join(",");

    $.post(ctx+"myCustomer/setLabelMyCustomer",{"uuid":uuidss,ids:idsAllss,"types":typess,label:label,pid:pidstr,idstr:idstr},function (r) {
        if (r.code == 0){
            $JuXue.n_success("设置成功");
            var a = ".theme-panel", b = "active";
            $(a).removeClass(b);
            $("#selectTheme").hide();
            $("#myCustomer-modal").empty().modal('hide');
            $JuXue.refreshTable('myEmpTable');
        }else{
            $JuXue.n_danger("设置失败");
        }
    });

}